package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.vo.ConsumeReportExportVO;
import com.cloudfun.campusshare.common.model.vo.MerchantReportFormExportListVO;
import com.cloudfun.campusshare.common.model.vo.OperatorProfileVo;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.ReportFormService;
import com.cloudfun.campusshare.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/5/12 14:01
 * @description : 报表
 */
@RestController
@RequestMapping("/v1/reportForm")
@Api(value = "报表")
@Validated
@Slf4j
public class ReportFormController {
    @Autowired
    private ReportFormService reportFormService;

    @ApiOperation(value = "运营商报表")
    @GetMapping("/merchant")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> operatorReport(@ApiParam(value = "时间过滤条件") @RequestParam(defaultValue = "1") Integer timeCon,
                                        @ApiParam(value = "设备种类") @RequestParam(required = false) List<String> deviceType) {
        return BaseResultFactory.produceSuccess(reportFormService.queryMerchantReportForm(timeCon, deviceType));
    }

    @ApiOperation(value = "消费报表")
    @GetMapping("/consume")
    @AuthRequired(role = {RoleType.ADMIN,RoleType.OPERATIONAL_MERCHANT})
    public BaseResult<?> orderReport(@ApiParam(value = "运营商id")@RequestParam(required = false) String merchantId,
                                     @ApiParam(value = "时间过滤条件") @RequestParam(defaultValue = "5") Integer timeCon,
                                     @ApiParam(value = "设备种类") @RequestParam(required = false) List<String> deviceType) {
        return BaseResultFactory.produceSuccess(reportFormService.queryOrderReportForm(merchantId, timeCon, deviceType));
    }

    @ApiOperation(value = "导出运营商报表")
    @GetMapping("/export/merchant")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> baseExportOperatorReport(HttpServletResponse response,
                                                  @ApiParam(value = "时间过滤条件") @RequestParam(defaultValue = "1") Integer timeCon,
                                                  @ApiParam(value = "设备种类") @RequestParam(required = false) List<String> deviceType) throws IOException {
        List<MerchantReportFormExportListVO> merchantReportFormExportListVOS = reportFormService.exportMerchantReportForm(timeCon, deviceType);
        ExcelUtils.exportExcel(merchantReportFormExportListVOS, "", "运营商报表", MerchantReportFormExportListVO.class, "运营商报表", response);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "导出消费报表")
    @GetMapping("/export/consume")
    @AuthRequired(role = {RoleType.ADMIN,RoleType.OPERATIONAL_MERCHANT})
    public BaseResult<?> baseExportOrderReport(HttpServletResponse response,
                                               @ApiParam(value = "运营商id") String merchantId,
                                               @ApiParam(value = "时间过滤条件") @RequestParam(defaultValue = "5") Integer timeCon,
                                               @ApiParam(value = "设备种类") @RequestParam(required = false) List<String> deviceType) throws IOException {
        List<ConsumeReportExportVO> consumeReportExportVOS = reportFormService.exportConsumeReportForm(merchantId, timeCon, deviceType);
        ExcelUtils.exportExcel(consumeReportExportVOS, "", "运营商报表", ConsumeReportExportVO.class, "订单报表", response);
        return BaseResultFactory.produceSuccess();
    }

    // 首页将会调用
    @ApiOperation(value = "运营商营收统计")
    @GetMapping("/om/revenueStatReport")
    @AuthRequired(role = {RoleType.OPERATIONAL_MERCHANT,RoleType.ADMIN})
    public BaseResult<?> orderReport(@ApiParam(value = "时间过滤条件") @RequestParam(defaultValue = "5") Integer timeCon,
                                     @ApiParam(value = "常用洗衣点") @RequestParam(required = false) String preferWashPointId) {
        return BaseResultFactory.produceSuccess(reportFormService.omQueryOrderReport(timeCon, preferWashPointId));
    }

    @ApiOperation(value = "运营商概况")
    @GetMapping("/operator/profile")
    @AuthRequired(role = {RoleType.ADMIN,RoleType.OPERATIONAL_MERCHANT})
    public BaseResult<OperatorProfileVo> operatorProfile(@ApiParam(value = "运营商id")@RequestParam(required = false) String merchantId){
        return BaseResultFactory.produceSuccess(reportFormService.operatorProfile(merchantId));
    }
}
